package com.xiefengf.elasticsearch.jd.utils;

import com.xiefengf.elasticsearch.jd.model.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiefengf
 * @version 1.0
 * @date 2024/4/6 22:08
 *
 * Jsoup 网页解析工具类
 * （用于爬取京东的数据）
 * 参考CSDN文档：https://blog.csdn.net/qq_62835094/article/details/132563662
 */
public class HtmlParseUtils {

//    public static void main(String[] args) {
//        List<Content> contentList = parseJD("Python");
//        for (Content content : contentList) {
//            System.out.println(content);
//        }
//    }

    /**
     * 解析京东网页数据，解析为对象返回
     * @param keywords 搜索词
     * @return
     * @throws IOException
     */
    public static List<Content> parseJD(String keywords) {
        try {
            // 1、获取请求地址 https://uland.taobao.com/sem/tbsearch?keyword=Python
            String url = "https://search.jd.com/Search?keyword="+keywords;
            // 2、设置cookie
            Map<String, String> cookies = new HashMap<String, String>();
            cookies.put("thor","3E083202F7EF0A5701252ABFF8F72EA0FBD3D55B18D4828BF887BE1D536F666C6434290AB8C37209D3C864212ED4EFCCC09D6EC2289A513BC8E580109ECD4D01D82583FB2E85306271486A72540CB98F02507C831C51C4B51E79070EA8162A37C50811BB413414C6928079562475FB4047133677AE266BF6F80CCEF1CFAD853B");
            // 3、解析网页
            Document document = Jsoup.connect(url).cookies(cookies).get();
            // 4、通过class获取ul标签
            Elements ul = document.getElementsByClass("gl-warp clearfix");
            // 5、获取ul标签下的所有li标签
            Elements liList = ul.select("li");
            // 6、解析数据+封装数据
            List<Content> list = new ArrayList<>();
            long id = 1001L;
            for (Element element : liList) {
                String title = element.getElementsByClass("p-name").first().text();
                String img = element.getElementsByTag("img").first().attr("data-lazy-img");
                String price = element.getElementsByClass("p-price").first().text();
                String shopName = element.getElementsByClass("p-shop").first().text();
                list.add(new Content(id++,title,img,price,shopName));
            }
            return list;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
